package com.music.mysongs.fragment.main

import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.music.mysongs.adapter.ViewPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.music.mysongs.R
import com.music.mysongs.utils.myLog
import kotlinx.android.synthetic.main.fragment_music.layout_more
import kotlinx.android.synthetic.main.layout_fragment_more.*

class MusicFragment : Fragment(), TabLayout.OnTabSelectedListener {
    private val mTAG = "MusicFragment"
    private var flagMore = true

    //定义布局控件
    private var mView: View? = null

    private var localListFragment = mutableListOf<Fragment>()
    private var localListTabLayout: TabLayout? = null
    private var localListPager: ViewPager? = null

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        layout_more.setOnClickListener {
            myLog(mTAG)
            layoutGroup_more.visibility = if (flagMore) {
                View.VISIBLE
            } else {
                View.GONE
            }
            flagMore = !flagMore
        }
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        mView = inflater.inflate(R.layout.fragment_music, container, false)
        initTabPager(mView!!)
        return mView
    }

    private fun initTabPager(v: View) {
        //布局控件赋值
        localListTabLayout = v.findViewById(R.id.layoutTab_music)
        localListPager = v.findViewById(R.id.layoutPager_music)
        //布局控件添加内容
        val mList = arrayOf("本地曲库", "喜欢的歌", "已传作品")
        for (i in mList.indices) {
            localListTabLayout!!.addTab(localListTabLayout!!.newTab().setText(mList[i]).setTag(i))
            localListFragment.add(MusicChildFragment(mList[i]))
        }
        /*  localListTabLayout!!.addTab(localListTabLayout!!.newTab().setText("本地").setTag(0))
          localListTabLayout!!.addTab(localListTabLayout!!.newTab().setText("喜欢").setTag(1))
          localListTabLayout!!.addTab(
              localListTabLayout!!.newTab().setIcon(R.drawable.ic_add_50).setTag(2)
          )*/
        localListTabLayout?.setupWithViewPager(localListPager)
        localListPager!!.adapter = ViewPagerAdapter(localListFragment, childFragmentManager)
        localListPager!!.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(localListTabLayout)
        )
        for (i in 0..2) {
            localListTabLayout!!.getTabAt(i)?.text = mList[i]
        }
        /*  localListTabLayout!!.getTabAt(0)?.text = "本地"
         localListTabLayout!!.getTabAt(1)?.text = "喜欢"
         localListTabLayout!!.getTabAt(2)?.icon = R.drawable.ic_add_50.toDrawable()*/
    }

    override fun onTabSelected(p0: TabLayout.Tab?) {
        TODO("Not yet implemented")
    }

    override fun onTabReselected(p0: TabLayout.Tab?) {
        TODO("Not yet implemented")
    }

    override fun onTabUnselected(p0: TabLayout.Tab?) {
        localListPager?.currentItem = p0!!.position
    }
}